package com.example.hw2;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class MyCustomDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        // 创建对话框布局
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.custom_dialog_layout, null);

        // 创建对话框选项按钮
        Button positiveButton = (Button) view.findViewById(R.id.positive_button);
        Button negativeButton = (Button) view.findViewById(R.id.negative_button);

        // 设置按钮点击监听器
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理“确定”按钮点击事件
                MyCustomDialog.this.dismiss(); // 关闭当前的弹出框
            }
        });

        negativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理“取消”按钮点击事件
                MyCustomDialog.this.dismiss();
            }
        });

        // 使用AlertDialog.Builder创建对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
                .setView(view) // 设置对话框布局
                .setPositiveButton(R.string.ok, null) // 设置“确定”按钮
                .setNegativeButton(R.string.cancel,null); // 设置“取消”按钮

        return builder.create(); // 创建并返回对话框
    }
}